
class Canopener extends FFWeapon;

#exec MESH IMPORT MESH=CanOpenerAnimated3Update ANIVFILE=MODELS\CanOpenerAnimated3Update_a.3d DATAFILE=MODELS\CanOpenerAnimated3Update_d.3d X=0 Y=0 Z=0 UnMirror=1
#exec MESH ORIGIN MESH=CanOpenerAnimated3Update X=35 Y=-130 Z=0 YAW=-68 PITCH=0 ROLL=0

#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=All       STARTFRAME=0    NUMFRAMES=200
#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=All       STARTFRAME=0    NUMFRAMES=47
#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Shoot1    STARTFRAME=1    NUMFRAMES=6
#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Shoot2    STARTFRAME=47   NUMFRAMES=4
#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Shoot3    STARTFRAME=91   NUMFRAMES=7

#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Unload1   STARTFRAME=8    NUMFRAMES=19
#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Unload2   STARTFRAME=51   NUMFRAMES=18
#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Unload3   STARTFRAME=98   NUMFRAMES=18

#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Load1     STARTFRAME=27   NUMFRAMES=19
#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Load2     STARTFRAME=69   NUMFRAMES=21
#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Load3     STARTFRAME=116  NUMFRAMES=19

#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Deselect1 STARTFRAME=136  NUMFRAMES=9
#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Deselect2 STARTFRAME=157  NUMFRAMES=10
#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Deselect3 STARTFRAME=178  NUMFRAMES=10

#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Select1   STARTFRAME=145  NUMFRAMES=10
#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Select2   STARTFRAME=167  NUMFRAMES=9
#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Select3   STARTFRAME=188  NUMFRAMES=10

#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Still1    STARTFRAME=155  NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Still2    STARTFRAME=176  NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerAnimated3Update SEQ=Still3    STARTFRAME=199  NUMFRAMES=1

#exec MESHMAP NEW MESHMAP=CanOpenerAnimated3Update MESH=CanOpenerAnimated3Update
#exec MESHMAP SCALE MESHMAP=CanOpenerAnimated3Update X=0.1 Y=0.1 Z=0.2

#exec TEXTURE IMPORT NAME=CanopenerUVMap  FILE=Textures/CanopenerUVMap.pcx          GROUP="Skins" LODSET=2
#exec TEXTURE IMPORT NAME=CanopenerUVMap2 FILE=Textures/CanopenerUVMap2.pcx         GROUP="Skins" LODSET=2
#exec TEXTURE IMPORT NAME=HandGlove       FILE=Textures/SimpleGloveHandTexture.pcx  GROUP="Skins" LODSET=2

#exec MESHMAP SETTEXTURE MESHMAP=CanOpenerAnimated3Update NUM=1 TEXTURE=CanopenerUVMap
#exec MESHMAP SETTEXTURE MESHMAP=CanOpenerAnimated3Update NUM=2 TEXTURE=CanopenerUVMap
#exec MESHMAP SETTEXTURE MESHMAP=CanOpenerAnimated3Update NUM=3 TEXTURE=HandGlove
#exec MESHMAP SETTEXTURE MESHMAP=CanOpenerAnimated3Update NUM=4 TEXTURE=HandGlove

//Left Handed Mesh
#exec MESH IMPORT MESH=CanOpenerAnimL ANIVFILE=MODELS\CanOpenerAnimated_a.3d DATAFILE=MODELS\CanOpenerAnimated_d.3d
#exec MESH ORIGIN MESH=CanOpenerAnimL X=15 Y=-80 Z=0 YAW=-64 PITCH=0 ROLL=0
#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=All STARTFRAME=0 NUMFRAMES=47

#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Shoot1 STARTFRAME=1 NUMFRAMES=6
#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Shoot2 STARTFRAME=47 NUMFRAMES=4
#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Shoot3 STARTFRAME=91 NUMFRAMES=7

#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Unload1 STARTFRAME=8 NUMFRAMES=19
#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Unload2 STARTFRAME=51 NUMFRAMES=18
#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Unload3 STARTFRAME=98 NUMFRAMES=18

#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Load1 STARTFRAME=27 NUMFRAMES=19
#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Load2 STARTFRAME=69 NUMFRAMES=21
#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Load3 STARTFRAME=116 NUMFRAMES=19

#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Deselect1 STARTFRAME=136 NUMFRAMES=9
#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Deselect2 STARTFRAME=157 NUMFRAMES=10
#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Deselect3 STARTFRAME=178 NUMFRAMES=10

#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Select1  STARTFRAME=145 NUMFRAMES=10
#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Select2 STARTFRAME=167 NUMFRAMES=9
#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Select3 STARTFRAME=188 NUMFRAMES=10

#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Still1 STARTFRAME=155 NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Still2 STARTFRAME=176 NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerAnimL SEQ=Still3 STARTFRAME=199 NUMFRAMES=1

#exec MESHMAP SCALE 		 MESHMAP=CanOpenerAnimL X=0.1 Y=0.1 Z=0.2
#exec MESHMAP SETTEXTURE MESHMAP=CanOpenerAnimL NUM=1 TEXTURE=CanopenerUVMap
#exec MESHMAP SETTEXTURE MESHMAP=CanOpenerAnimL NUM=2 TEXTURE=CanopenerUVMap
#exec MESHMAP SETTEXTURE MESHMAP=CanOpenerAnimL NUM=2 TEXTURE=HandGlove

// PICKUP (Removed and the 3rd Mesh used)
/*
#exec MESH IMPORT MESH=CanOpenerPickup ANIVFILE=MODELS\CanOpenerAllCans_a.3d DATAFILE=MODELS\CanOpenerAllCans_d.3d X=0 Y=0 Z=0 UNMIRROR=1
#exec MESH ORIGIN MESH=CanOpenerPickup X=0 Y=0 Z=-110 YAW=0 PITCH=0 ROLL=0
#exec MESH SEQUENCE MESH=CanOpenerPickup SEQ=All     STARTFRAME=0  NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerPickup SEQ=Still1        STARTFRAME=0   NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerPickup SEQ=Still2        STARTFRAME=0   NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerPickup SEQ=Still3        STARTFRAME=0   NUMFRAMES=1
#exec MESHMAP NEW MESHMAP=CanOpenerPickup MESH=CanOpenerPickup
#exec MESH LODPARAMS MESH=CanOpenerPickup STRENGTH=0
#exec MESHMAP SCALE MESHMAP=CanOpenerPickup X=0.0375 Y=0.0375 Z=0.075
#exec MESHMAP SETTEXTURE MESHMAP=CanOpenerPickup NUM=1 TEXTURE=CanopenerUVMap2
#exec MESHMAP SETTEXTURE MESHMAP=CanOpenerPickup NUM=2 TEXTURE=CanopenerUVMap
*/

// 3RD PERSON
#exec MESH IMPORT MESH=CanOpenerBean ANIVFILE=MODELS\CanOpenerBean_a.3d DATAFILE=MODELS\CanOpenerBean_d.3d X=0 Y=0 Z=0 LODSTYLE=10 UnMirror=1
#exec MESH ORIGIN MESH=CanOpenerBean X=0 Y=-350 Z=0 YAW=-64 PITCH=0 ROLL=0
#exec MESH SEQUENCE MESH=CanOpenerBean SEQ=All     STARTFRAME=0  NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerBean SEQ=Still1        STARTFRAME=0   NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerBean SEQ=Still2        STARTFRAME=0   NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerBean SEQ=Still3        STARTFRAME=0   NUMFRAMES=1
#exec MESHMAP NEW MESHMAP=CanOpenerBean MESH=CanOpenerBean
#exec MESH LODPARAMS MESH=CanOpenerBean STRENGTH=0
#exec MESHMAP SCALE MESHMAP=CanOpenerBean X=0.02 Y=0.02 Z=0.04
#exec MESHMAP SETTEXTURE MESHMAP=CanOpenerBean NUM=1 TEXTURE=CanopenerUVMap
#exec MESHMAP SETTEXTURE MESHMAP=CanOpenerBean NUM=2 TEXTURE=CanopenerUVMap

#exec MESH IMPORT MESH=CanOpenerCherry ANIVFILE=MODELS\CanOpenerCherry_a.3d DATAFILE=MODELS\CanOpenerCherry_d.3d X=0 Y=0 Z=0 LODSTYLE=10 UnMirror=1
#exec MESH ORIGIN MESH=CanOpenerCherry X=0 Y=-350 Z=0 YAW=-64 PITCH=0 ROLL=0
#exec MESH SEQUENCE MESH=CanOpenerCherry SEQ=All     STARTFRAME=0  NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerCherry SEQ=Still1        STARTFRAME=0   NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerCherry SEQ=Still2        STARTFRAME=0   NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerCherry SEQ=Still3        STARTFRAME=0   NUMFRAMES=1
#exec MESHMAP NEW MESHMAP=CanOpenerCherry MESH=CanOpenerCherry
#exec MESH LODPARAMS MESH=CanOpenerCherry STRENGTH=0
#exec MESHMAP SCALE MESHMAP=CanOpenerCherry X=0.02 Y=0.02 Z=0.04
#exec MESHMAP SETTEXTURE MESHMAP=CanOpenerCherry NUM=1 TEXTURE=CanopenerUVMap
#exec MESHMAP SETTEXTURE MESHMAP=CanOpenerCherry NUM=2 TEXTURE=CanopenerUVMap

#exec MESH IMPORT MESH=CanOpenerPepper ANIVFILE=MODELS\CanOpenerPepper_a.3d DATAFILE=MODELS\CanOpenerPepper_d.3d X=0 Y=0 Z=0 LODSTYLE=10 UnMirror=1
#exec MESH ORIGIN MESH=CanOpenerPepper X=0 Y=-350 Z=0 YAW=-64 PITCH=0 ROLL=0
#exec MESH SEQUENCE MESH=CanOpenerPepper SEQ=All     STARTFRAME=0  NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerPepper SEQ=Still1        STARTFRAME=0   NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerPepper SEQ=Still2        STARTFRAME=0   NUMFRAMES=1
#exec MESH SEQUENCE MESH=CanOpenerPepper SEQ=Still3        STARTFRAME=0   NUMFRAMES=1
#exec MESHMAP NEW MESHMAP=CanOpenerPepper MESH=CanOpenerPepper
#exec MESH LODPARAMS MESH=CanOpenerPepper STRENGTH=0
#exec MESHMAP SCALE MESHMAP=CanOpenerPepper X=0.02 Y=0.02 Z=0.04
#exec MESHMAP SETTEXTURE MESHMAP=CanOpenerPepper NUM=1 TEXTURE=CanopenerUVMap
#exec MESHMAP SETTEXTURE MESHMAP=CanOpenerPepper NUM=2 TEXTURE=CanopenerUVMap

//Sounds
#exec AUDIO IMPORT FILE="Sounds\canopenerGeneric.WAV" NAME="canopenerGeneric" GROUP="CanOpener"
#exec AUDIO IMPORT FILE="Sounds\canopenerReload.WAV" NAME="canopenerReload" GROUP="CanOpener"
#exec AUDIO IMPORT FILE="Sounds\canopenerHalpFire.WAV" NAME="canopenerHalpFire" GROUP="CanOpener"
#exec AUDIO IMPORT FILE="Sounds\CanOpenerPickup.WAV" NAME="CanOpenerPickup" GROUP="CanOpener"

//Icon
#exec TEXTURE IMPORT NAME=CanOpenerIcon FILE=Textures/CanOpenerIcon.BMP GROUP="Icons" MIPS=OFF

var int currentAmmo;
var() localized class<ammo> AmmoNames[3];

var() localized name SelectAnim[3];
var() localized name DeselectAnim[3];
var() localized name ShootAnim[3];
var() localized name LoadAnim[3];
var() localized name UnloadAnim[3];
var() localized name StillAnim[3];
var() localized Mesh StillThirdPersons[3];

var int currentAmmoBeforeTryingSwitch;
var Ammo ammoBeforeTryingSwitch;

var() int hitdamage;
var  float AltAccuracy;
var bool bFirstFire;
var() texture MuzzleFlashVariations[5];
var() sound ShootSounds[3];

var int clientAmmo, clientOldAmmoType, serverOldAmmo, serverOldAmmoType;
var float fireRate;
var bool affectCurAmmoType;

replication
{
   reliable if (Role == ROLE_Authority && bNetOwner)
      currentAmmo, fireRate;
   reliable if (Role == ROLE_Authority)
      AddClientAmmo;
}

function bool HandlePickupQuery( inventory Item )
{
	local Pawn P;
	local Ammo ammunition;
	local int OldAmmo;
	if ( (Item.class == class) )
	{
		if ( Weapon(item).bWeaponStay && (!Weapon(item).bHeldItem || Weapon(item).bTossedOut) )
			return true;
		P = Pawn(Owner);
		ammunition = Ammo(p.FindInventoryType(Weapon(Item).AmmoName));
		if ( ammunition != None ) 
		{
			OldAmmo = ammunition.AmmoAmount;
			if ( ammunition.AddAmmo(Weapon(Item).PickUpAmmoCount) && (OldAmmo == 0) && (P.Weapon.class != item.class) && !P.bNeverSwitchOnPickup )
			{
				WeaponSet(P);
			}
		}
		else
		{
			ammunition = Spawn(Weapon(Item).AmmoName);
			P.AddInventory(ammunition);
			ammunition.BecomeItem();
			ammunition.AmmoAmount = PickUpAmmoCount; 
		}
		if (!bWeaponUp && (AmmoType == None || AmmoType.AmmoAmount == 0))
		{
			Gotostate('AltFiring');
		}
		P.ReceiveLocalizedMessage( class'PickupMessagePlus', 0, None, None, Self.Class );
		Item.PlaySound(Item.PickupSound);
		if (Level.Game.LocalLog != None)
			Level.Game.LocalLog.LogPickup(Item, Pawn(Owner));
		if (Level.Game.WorldLog != None)
			Level.Game.WorldLog.LogPickup(Item, Pawn(Owner));
		Item.SetRespawn();
		return true;
	}
	if ( Inventory == None )
		return false;

	return Inventory.HandlePickupQuery(Item);
}

function Weapon WeaponChange( byte F )
{	
	local Pawn P;

	P = Pawn(Owner);
	if(InventoryGroup == F ) 
	{
		if ((AmmoType != none && AmmoType.AmmoAmount > 0) || switchAmmo(1)) 
		{
			return self;
		} 
		else 
		{
			return none;
		}
	}
	super.WeaponChange(F);
}

function float SwitchPriority()
{
	local float temp;
	local int bTemp;

	if ( !Owner.IsA('PlayerPawn') )
		return RateSelf(bTemp);
	else if ( (AmmoType != None) && (AmmoType.AmmoAmount<=0) )
	{
		if ( Pawn(Owner).Weapon == self )
			return -0.5;
		else
			return -1;
	}
	else return AutoSwitchPriority;
}

function ProcessTraceHit(Actor Other, Vector HitLocation, Vector HitNormal, Vector X, Vector Y, Vector Z)
{
	local vector realLoc;

	realLoc = Owner.Location + CalcDrawOffset();
	
	if (Other == Level)
   	Spawn(class'BeanWallHitEffect',,, HitLocation+HitNormal, Rotator(HitNormal));

	if ((Other != self) && (Other != Owner) && (Other != None) )
	{
		if ( FRand() < 0.2 )
			X *= 5;
		Other.TakeDamage(HitDamage, Pawn(Owner), HitLocation, 3000.0*X, MyDamageType);
		if ( !Other.bIsPawn && !Other.IsA('Carcass') )
			spawn(class'UT_SpriteSmokePuff',,,HitLocation+HitNormal*9);
		else
			Other.PlaySound(Sound 'ChunkHit',, 4.0,,100);
	}
	ProcessTraceHitEffect(Other, ConsumableAmmo(AmmoType), HitLocation, HitNormal);
}

function setHand(float Hand)
{
	if ( Hand == 2 )
   {
      bHideWeapon = true;
      Super.SetHand(Hand);
      return;
   }
   Super.SetHand(Hand);
	if ( Hand == 1 )
		Mesh = mesh'CanOpenerAnimL';
	else
		Mesh = mesh'CanOpenerAnimated3Update';
}

function TraceFire( float Accuracy )
{
	local vector RealOffset;

	RealOffset = FireOffset;
	FireOffset *= 0.35;
	if ( Owner.IsA('Bot') && !Bot(Owner).bNovice )
		Accuracy = FMax(Accuracy, 0.45);
	Super.TraceFire(Accuracy);
	FireOffset = RealOffset;
}

function NewTraceFire( float Accuracy )
{
    local vector HitLocation, HitNormal, StartTrace, EndTrace, X,Y,Z, AimDir;
    local actor Other;

    Owner.MakeNoise(Pawn(Owner).SoundDampening);
    GetAxes(Pawn(owner).ViewRotation,X,Y,Z);
    StartTrace = Owner.Location + CalcDrawOffset() + FireOffset.Y * Y + FireOffset.Z * Z; 
    AdjustedAim = pawn(owner).AdjustAim(1000000, StartTrace, 2.75*AimError, False, False);  
    EndTrace = StartTrace + Accuracy * (FRand() - 0.5 )* Y * 1000
        + Accuracy * (FRand() - 0.5 ) * Z * 1000;
    AimDir = vector(AdjustedAim);
    EndTrace += (10000 * AimDir); 
    Other = Pawn(Owner).TraceShot(HitLocation,HitNormal,EndTrace,StartTrace);

   if ( VSize(HitLocation - StartTrace) > 150 )
      Spawn(class'HalpTracer',,, StartTrace + 96 * AimDir,rotator(EndTrace - StartTrace));
}

function Fire(float Value)
{
	if ( AmmoType == None )
		GiveAmmo(Pawn(Owner));

	if (AmmoType.UseAmmo(1))
	{
		bCanClientFire = True;
		bPointing = True;
		ClientFire(Value);
		Pawn(Owner).PlayRecoil(FiringSpeed);
		TraceFire(0.2);
		GotoState('NormalFire');
	}
}

simulated function PlayFiring()
{
	PlayOwnedSound(ShootSounds[currentAmmo], SLOT_None,2.0*Pawn(Owner).SoundDampening);
	bMuzzleFlash++;
	PlayAnim(ShootAnim[currentAmmo], fireRate, 0.02);
}

function AltFire( float Value )
{
   bCanClientFire = True;
   bPointing=True;
   ClientAltFire(Value);
   GotoState('AltFiring');
   switchAmmo(1);
}

function bool switchAmmo(int tries) 
{
	local Pawn p;

	if (tries == 1) 
	{
		ammoBeforeTryingSwitch = AmmoType;
		currentAmmoBeforeTryingSwitch = currentAmmo;
	}
	p = Pawn(Owner);
	if(tries > 2) 
	{
		AmmoType = ammoBeforeTryingSwitch;
		currentAmmo = currentAmmoBeforeTryingSwitch;
		//p.ClientMessage("No more ammo!!", 'Pickup');
		return false;
	}
	if(currentAmmo == 2) 
	{
		currentAmmo = -1;
	}
	currentAmmo++;
	AmmoType = Ammo(p.FindInventoryType(AmmoNames[currentAmmo]));
	if(AmmoType == None || AmmoType.AmmoAmount <= 0)
	{
		return switchAmmo(tries + 1);
	} 
	else 
	{
		ammoBeforeTryingSwitch.Gotostate('Idle2');
		AmmoName = AmmoNames[currentAmmo];
		ThirdPersonMesh = StillThirdPersons[currentAmmo];
		if (currentAmmo == 0) 
		{
			fireRate=0.800000;
			HitDamage=17;
			FireSound=Sound'canopenerGeneric';
		} 
		else if (currentAmmo == 1) 
		{
			fireRate=1.500000;
			HitDamage=10;
			FireSound=Sound'canopenerGeneric';
		} 
		else if (currentAmmo == 2)
		{
			fireRate=0.400000;
			HitDamage=100;
			FireSound=Sound'canopenerHalpFire';
		}
		return true;
	}
}

simulated function PlayAltFiring()
{
   if (hasMoreAmmo())
   {
      PlayOwnedSound(AltFireSound, SLOT_None, 2.0*Pawn(Owner).SoundDampening);
      PlayAnim(UnloadAnim[currentAmmo]);
   }
   else
      TweenToStill();
}


simulated function PlaySelect()
{
	bForceFire = false;
	bForceAltFire = false;
	bCanClientFire = false;
	if ( !IsAnimating() || (!isSelectAnim(AnimSequence)) )
		PlayAnim(SelectAnim[currentAmmo],1.0,0.0);
	Owner.PlaySound(SelectSound, SLOT_Misc, Pawn(Owner).SoundDampening);
}

simulated function bool isSelectAnim(name anim)
{
	local byte i;

   for (i = 0; i < ArrayCount(SelectAnim); i++)
   {
      if (anim == SelectAnim[i])
         return True;
   }
   return False;
}

simulated function TweenDown()
{
	if ( IsAnimating() && (AnimSequence != ''))
		TweenAnim( AnimSequence, AnimFrame * 0.4 );
	else
		PlayAnim(DeselectAnim[currentAmmo], 1.0, 0.05);
}

function GiveAmmo(Pawn p)
{
	super.GiveAmmo(p);
	switchAmmo(1);
}

event float BotDesireability(Pawn Bot)
{
	local Canopener AlreadyHas;
	local float desire;

	desire = MaxDesireability + Bot.AdjustDesireFor(self);
	AlreadyHas = Canopener(Bot.FindInventoryType(class));
	if ( AlreadyHas != None )
   {
		if ( (!bHeldItem || bTossedOut) && bWeaponStay )
			return 0;
		if ( (RespawnTime < 10) && ( bHidden || (AlreadyHas.AmmoType == None)
      	|| (AlreadyHas.AmmoType.AmmoAmount < AlreadyHas.AmmoType.MaxAmmo)) )
         return 0;
		if ( AlreadyHas.AmmoType == None )
         return 0.25 * desire;
      if ( AlreadyHas.AmmoType.AmmoAmount > 0 )
         return FMax( 0.25 * desire, AlreadyHas.AmmoType.MaxDesireability
            * FMin(1, 0.15 * AlreadyHas.AmmoType.MaxAmmo/AlreadyHas.AmmoType.AmmoAmount) );
	}

	if ( (Bot.Weapon == None) || (Bot.Weapon.AIRating <= 0.4) )
		return 2*desire;

	return desire;
}

simulated event RenderOverlays(canvas Canvas)
{
	local PlayerPawn PlayerOwner;
	local int realhand;

	if ( (bMuzzleFlash > 0) && !Level.bDropDetail )
		MFTexture = MuzzleFlashVariations[Rand(5)];
	PlayerOwner = PlayerPawn(Owner);
	if ( PlayerOwner != None )
	{
		if ( PlayerOwner.DesiredFOV != PlayerOwner.DefaultFOV )
			return;
		realhand = PlayerOwner.Handedness;
		if (  (Level.NetMode == NM_Client) && (realHand == 2) )
		{
			bHideWeapon = true;
			return;
		}
		if ( !bHideWeapon )
		{
			if ( Mesh == mesh'CanOpenerAnimL' )
				PlayerOwner.Handedness = 1;
		}
	}
	if ( (PlayerOwner == None) || (PlayerOwner.Handedness == 0) )
	{
		FlashO = 1.9 * Default.FlashO;
		FlashY = Default.FlashY;
	}
	else
	{
		FlashO = 0.6 * Default.FlashO;
		FlashY = Default.FlashY;
	}
	Super.RenderOverlays(Canvas);
	if ( PlayerOwner != None )
		PlayerOwner.Handedness = realhand;
}

simulated function bool isUnloadAnim(name anim)
{
local byte i;

   for (i = 0; i < ArrayCount(UnloadAnim); i++)
   {
      if (anim == UnloadAnim[i])
         return True;
   }
   return False;
}

simulated function bool isShootAnim(name anim)
{
local byte i;

   for (i = 0; i < ArrayCount(ShootAnim); i++)
   {
      if (anim == ShootAnim[i])
         return True;
   }
   return False;
}

simulated function PlayReloading()
{
	Gotostate('Reloading');
}

state Reloading
{
	ignores Fire, AltFire;

Begin:
	FinishAnim();
	Finish();
}

state Active
{
	function bool PutDown()
	{
		if ( bWeaponUp || (AnimFrame < 0.75) ) 
			GotoState('DownWeapon');
		else
			bChangeWeapon = true;
		return True;
	}

	function BeginState()
	{
		bChangeWeapon = false;
	}

Begin:
	FinishAnim();
	PlayAnim(StillAnim[currentAmmo],0.1,0.1);
	if ( bChangeWeapon )
		GotoState('DownWeapon');
	bWeaponUp = True;
	bCanClientFire = true;
	if ( (Level.Netmode != NM_Standalone) && (Owner != None)	&& Owner.IsA('TournamentPlayer')
		&& (PlayerPawn(Owner).Player != None) && !PlayerPawn(Owner).Player.IsA('ViewPort') )
	{
		if ( Pawn(Owner).bFire != 0 )
			TournamentPlayer(Owner).SendFire(self);
		else if ( !bChangeWeapon )
			TournamentPlayer(Owner).UpdateRealWeapon(self);
	} 
	Finish();
}

State DownWeapon
{
	ignores Fire, AltFire, Animend;

End:
	bWeaponUp = False;
}

state Idle
{
	function bool PutDown()
	{
		GotoState('DownWeapon');
		return True;
	}
	
Begin:
	bPointing=False;
	if ( (AmmoType != None) && (AmmoType.AmmoAmount<=0) && !switchAmmo(1)) 
	{
		Pawn(Owner).SwitchToBestWeapon();
	}
	if ( Pawn(Owner).bFire!=0 ) Global.Fire(0.0);
	if ( Pawn(Owner).bAltFire!=0 ) Global.AltFire(0.0);	
}

state NormalFire
{
Begin:
   if (AmmoType.IsA('HalapenjoCan'))
      NewTraceFire(0.1);
   if (AmmoType != None && AmmoType.AmmoAmount == 0 && hasMoreAmmo(True))
   {
      affectCurAmmoType = True;
      Gotostate('AltFiring');
   }
}

state AltFiring
{
   function AnimEnd()
   {
      if (hasMoreAmmo(affectCurAmmoType))
      {
         if (isShootAnim(AnimSequence))
         {
            PlayAnim(UnloadAnim[currentAmmo], 0.7);
            switchAmmo(1);
         }
         else if (isUnloadAnim(AnimSequence))
            PlayAnim(LoadAnim[currentAmmo], 0.7);
         else
         {
            affectCurAmmoType = False;
            Super.AnimEnd();
         }
      }
      else
         Super.AnimEnd();
   }
}

state Switching
{
	ignores AltFire;

Begin:
   switchAmmo(1);
   FinishAnim();
   PlayAnim(LoadAnim[currentAmmo], 0.7);
End:
   Gotostate('Active');
}

state ClientAltFiring
{
   simulated function AnimEnd()
   {
      if (hasMoreAmmo(affectCurAmmoType))
      {
         if (isShootAnim(AnimSequence))
            PlayAnim(UnloadAnim[currentAmmo], 0.7);
         else if (isUnloadAnim(AnimSequence))
            PlayAnim(LoadAnim[currentAmmo], 0.7);
         else
         {
            UpdateClientAmmo();
            affectCurAmmoType = False;
            Super.AnimEnd();
         }
      }
      else
         Super.AnimEnd();
   }
}

state ClientFiring
{
   simulated function AnimEnd()
   {
      if (isShootAnim(AnimSequence))
      {
         clientAmmo--;
         if (clientAmmo <= 0)
         {
            AmmoType.AmmoAmount = 0;
            if (hasMoreAmmo(True))
            {
            	PlayOwnedSound(AltFireSound, SLOT_None, 2.0*Pawn(Owner).SoundDampening);
               PlayAnim(UnloadAnim[currentAmmo], 0.7);
               affectCurAmmoType = True;
               Gotostate('ClientAltFiring');
            }
            else
               Super.AnimEnd();
         }
         else
            Super.AnimEnd();
      }
      else
         Super.AnimEnd();
   }
       
   simulated function BeginState()
   {
      UpdateClientAmmo();
      Super.BeginState();
   }
}

simulated function TweenToStill()
{
    TweenAnim(StillAnim[currentAmmo],0.1);
}

simulated function Tick(float Delta)
{
   Super.Tick(Delta);
   if (Role == ROLE_Authority && Level.NetMode != NM_StandAlone && AmmoType != None)
   {
      if (serverOldAmmoType == currentAmmo && AmmoType.AmmoAmount > serverOldAmmo)
         AddClientAmmo(AmmoType.AmmoAmount - serverOldAmmo);
      serverOldAmmoType = currentAmmo;
      serverOldAmmo = AmmoType.AmmoAmount;
   }
}

simulated function AddClientAmmo(int n)
{
   clientAmmo += n;
}

simulated function UpdateClientAmmo()
{
   if (clientOldAmmoType != currentAmmo)
   {
      clientOldAmmoType = currentAmmo;
      clientAmmo = AmmoType.AmmoAmount;
   }
}

simulated function bool hasMoreAmmo(optional bool ammoTypeCheck)
{
	local Pawn P;
   local Ammo Am;
   local byte i;

   P = Pawn(Owner);
   if (P == None)
      return False;
   for (i = 0; i < ArrayCount(AmmoNames); i++)
   {
      Am = Ammo(P.FindInventoryType(AmmoNames[i]));
      if ((ammoTypeCheck || Am != AmmoType) && Am != None && Am.AmmoAmount > 0)
         return True;
   }
   return False;
}

defaultproperties
{
   ItemName="Canopener"
   PickupMessage="You picked up a Canopener!"
   hitdamage=17
   AmmoNames(0)=Class'FoodFightWeapons.KidneyBeanCan'
   AmmoNames(1)=Class'FoodFightWeapons.SweetCherryCan'
   AmmoNames(2)=Class'FoodFightWeapons.HalapenjoCan'
   AmmoName=Class'FoodFightWeapons.KidneyBeanCan'
   currentAmmo=0;
   MuzzleFlashVariations(0)=Texture'Botpack.Skins.Muz1'
   MuzzleFlashVariations(1)=Texture'Botpack.Skins.Muz2'
   MuzzleFlashVariations(2)=Texture'Botpack.Skins.Muz3'
   MuzzleFlashVariations(3)=Texture'Botpack.Skins.Muz4'
   MuzzleFlashVariations(4)=Texture'Botpack.Skins.Muz5'
   WeaponDescription="Classification: Tin can opener\n\nPrimary Fire: Based on the ammo type chosen, fires a single, accurate bean to the opponent.\n\nSecondary Fire: Switch ammo to any one of the three types - Kidney beans, Sweet cherry and Halapenjo. \n\nTechniques: \n Kidney beans - normal mode.\n The sweet cherry - Many bullets and fast firing mode, but less damage as the beans.\n Halapenjo (very rare) - Only 7 bullets, but one shot, one kill."
   InstFlash=-0.200000
   InstFog=(X=325.000000,Y=225.000000,Z=95.000000)
   PickupAmmoCount=30
   bInstantHit=True
   bAltInstantHit=True
   FiringSpeed=1.500000
   FireOffset=(Y=-10.000000,Z=-4.000000)
   MyDamageType=shot
   shakemag=200.000000
   shakevert=4.000000
   AIRating=0.250000
   fireRate=0.800000
   AltRefireRate=0.870000
   FireSound=Sound'canopenerGeneric'
   AltFireSound=Sound'canopenerReload'
   CockingSound=Sound'Botpack.enforcer.Cocking'
   SelectSound=Sound'Botpack.enforcer.Cocking'
   DeathMessage="%k emptied whole can of beans over %o with the %w."
   NameColor=(R=200,G=200)
   bDrawMuzzleFlash=True
   MuzzleScale=1.000000
   FlashY=0.100000
   FlashO=0.020000
   FlashC=0.035000
   FlashLength=0.020000
   FlashS=128
   MFTexture=Texture'Botpack.Skins.Muz1'
   AutoSwitchPriority=2
   InventoryGroup=2
   PlayerViewOffset=(X=3.300000,Y=-2.000000,Z=-3.000000)
   PlayerViewMesh=CanOpenerAnimated3Update
   PickupViewMesh=CanOpenerBean
   ThirdPersonMesh=CanOpenerBean
   StatusIcon=Texture'CanOpenerIcon'
   bMuzzleFlashParticles=True
   MuzzleFlashStyle=STY_Translucent
   MuzzleFlashMesh=LodMesh'Botpack.muzzEF3'
   MuzzleFlashScale=0.080000
   MuzzleFlashTexture=Texture'Botpack.Skins.Muzzy2'
   PickupSound=Sound'CanOpenerPickup'
   Icon=Texture'Botpack.Icons.UseAutoM'
   bHidden=True
   Mesh=LodMesh'FoodFightWeapons.CanOpenerBean'
   bNoSmooth=False
   CollisionRadius=24.000000
   CollisionHeight=16.000000
   Mass=15.000000
   ShootAnim(0)=Shoot1
   ShootAnim(1)=Shoot2
   ShootAnim(2)=Shoot3
   LoadAnim(0)=Load1
   LoadAnim(1)=Load2
   LoadAnim(2)=Load3
   UnloadAnim(0)=Unload1
   UnloadAnim(1)=Unload2
   UnloadAnim(2)=Unload3
   SelectAnim(0)=Select1
   SelectAnim(1)=Select2
   SelectAnim(2)=Select3
   DeselectAnim(0)=Deselect1
   DeselectAnim(1)=Deselect2
   DeselectAnim(2)=Deselect3
   StillAnim(0)=Still1
   StillAnim(1)=Still2
   StillAnim(2)=Still3
   StillThirdPersons(0)=CanOpenerBean
   StillThirdPersons(1)=CanOpenerCherry
   StillThirdPersons(2)=CanOpenerPepper
   ShootSounds(0)=Sound'canopenerGeneric'
	ShootSounds(1)=Sound'canopenerGeneric'
	ShootSounds(2)=Sound'canopenerHalpFire'
	clientOldAmmoType=10
   serverOldAmmoType=10
   fireRate=0.800000
}
